<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChRoomCode;
use Carbon\Carbon;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use function Hyperf\Collection\data_get;

class ChRoomCodeMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChRoomCode::class;
    }

    public function findById(int $id)
    {
        return $this->model::find($id)->first();
    }


    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query;
    }

    public function handleSearch2(Builder $query, array $params): Builder
    {
        $code_name = data_get($params, 'code_name');
        $main_uid = data_get($params, 'main_uid');
        $account_id = data_get($params, 'account_id');
        $status = data_get($params, 'status');
        $visit_type = data_get($params, 'visit_type');
        $startTime = data_get($params, 'start_time');
        $endTime = data_get($params, 'end_time');
        $create_user_id = data_get($params, 'create_user_id');

        if ($visit_type) {
            $query->where('rc.visit_type', $visit_type);
        }
        if ($main_uid) {
            $query->where('rc.main_uid', $main_uid);
        }
        if ($account_id) {
            $query->where('rc.uid', $account_id);
        }
        if (filled($status)) {
            $query->where('rc.status', $status);
        }
        if ($code_name) {
            $query->where('rc.code_name', 'like', '%' . $code_name . '%');
        }
        $query->when(!empty($startTime) && !empty($endTime), function ($query) use ($startTime, $endTime) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        });

        if($create_user_id){
            $query->where('rcr.create_user_id', $create_user_id);
        }
        return $query;
    }


}